<?php

abstract class Substitutions
{
    public function Process( $data )
    {
        if ( is_array($data) )
        {
            $result = array();
            foreach ( $data as $key => $value )
            {
                $result[$key] = $this->ProcessString($value);
            }
            return $result;
        }

        if ( is_string($data) )
        {
            return $this->ProcessString($data);
        }

        return $data;
    }

    public function ProcessString( $string )
    {
        if ( strpos($string, "#") !== false )
        {
            if ( preg_match_all("/#([A-Z0-9_]+)#/", $string, $matches, PREG_SET_ORDER) )
            {
                foreach ($matches as $match)
                {
                    $key = $match[1];
                    $value = $this->GetValue($key);
                    if ( !is_null($value) )
                    {
                        $string = str_replace("#$key#", $value, $string);
                    }
                }
            }
        }
        return $string;
    }

    protected abstract function GetValue( $key );
}

?>
